home *** CD-ROM | disk | FTP | other *** search
/ Chip 2003 October / Chip Ekim 2003.iso / prog / code / contr / setup.exe / Disk1 / data1.cab / Configuration_En / Commands / PasteFireworksHTML.js < prev    next >
Encoding:
JavaScript  |  2003-07-18  |  5.1 KB  |  180 lines

  1. // Copyright 2000, 2001, 2002, 2003 Macromedia, Inc. All rights reserved.
  2.  
  3. //*************** GLOBALS  *****************
  4.  
  5. var USE_SITE_RELATIVE = false;
  6.  
  7.  
  8. //******************* LOCAL FUNCTIONS **********************
  9.  
  10. function isDOMRequired() {
  11.   // Return false, indicating that this object is available in code view.
  12.   // This will prevent the document from syncing with design view on every
  13.   // paste in code view.  This command actually does require that the DOM
  14.   // be synchronized, but we'll postpone that until we're sure that we're
  15.   // actually pasting Fireworks HTML.
  16.   return false;
  17. }
  18.  
  19. function initialize() {
  20. var validFWFile = false;
  21. var fwSource = dw.getClipboardText();
  22.   if (fwSource && isFireworksHTML(fwSource)) {
  23.     validFWFile = true;
  24.       MM.event.handled = pasteFWHTML();
  25.   }else{
  26.     MM.event.handled = false;
  27.   }
  28. }
  29.  
  30.  
  31. function pasteFWHTML() {
  32.   var result, validFWFile=false;
  33.   var fwURL, fwSource, fwDOM;
  34.   var docURL, docDOM, siteURL, parentFile, docRootURL, siteRootURL;
  35.  
  36.   // Force a sync... we have postponed this until we are certain that we're
  37.   // actually pasting Fireworks HTML.
  38.   if (!dw.getDocumentDOM().isDesignViewUpdated())
  39.       dw.getDocumentDOM().synchronizeDocument();
  40.  
  41.   fwSource = dw.getClipboardText();
  42.  
  43.   if (fwSource && isFireworksHTML(fwSource)) {
  44.     validFWFile = true;
  45.     if (!isDWStyle(fwSource))
  46.       alert(MSG_notExportedForDW);
  47.     else if (!usesDWBehaviors(fwSource))
  48.       alert(MSG_behNotSupported);
  49.   }
  50.  
  51.   if (validFWFile) {
  52.     fwURL = dw.getConfigurationPath() + "/Shared/MM/Cache/empty.htm";
  53.     DWfile.write(fwURL,'');
  54.     fwDOM = dw.getDocumentDOM(fwURL);
  55.     
  56.     //for i18n, we need to make sure the fwDOM matches the encoding of the users dom
  57.       fwDOM.setCharSet( dw.getDocumentDOM('document').getCharSet() );
  58.   
  59.     // remove Content-Type metas from fwSource before sticking source in fwDOM
  60.     // (there should already be a meta in Dreamweaver, so removing this one will
  61.     // prevent a duplicate meta being added).
  62.     fwSource = fwSource.replace(/<meta\s+http-equiv="Content-Type[^>]*>[\r\n\s]*/i,'');
  63.     
  64.     fwDOM.documentElement.outerHTML = fwSource;
  65.         
  66.     docURL = dw.getDocumentPath("document");
  67.     siteURL = dw.getSiteRoot();
  68.  
  69.     docRootURL = '';
  70.     siteRootURL = '';
  71.     if (docURL) {
  72.       parentFile = new File(docURL);
  73.       docRootURL = parentFile.getAbsoluteParent() + File.separator;
  74.       if (USE_SITE_RELATIVE) {
  75.         parentFile = new File(siteURL);
  76.         siteRootURL = parentFile.getAbsolutePath();
  77.       }
  78.     }
  79.     
  80.     docDOM = dw.getDocumentDOM('document');
  81.  
  82.     // The Insert FWHTML Object fixes up the caret to make sure it's
  83.     // in the body before inserting the HTML returned by insertFireworksHTML.
  84.     // Paste doesn't do this, so we're going to do it manually. We
  85.     // start by checking to make sure the IP is in the body. If it isn't,
  86.     // we move it to the body.
  87.     if (!dwscripts.isInsideTag(docDOM.getSelectedNode(),"BODY")){
  88.       var bodyNode = docDOM.body;
  89.       var bodyOffsets = docDOM.nodeToOffsets(bodyNode);
  90.       docDOM.setSelection(bodyOffsets[1]+1,bodyOffsets[1]+1);
  91.     }
  92.     
  93.     // Next we save the current selection relative to the body tag.
  94.     saveBodyRelativeSelection();
  95.  
  96.     theHTML = insertFireworksHTML(fwDOM, fwURL, docRootURL, siteRootURL);
  97.   
  98.     // We're done with the fwDOM. Release it.
  99.     dw.releaseDocument(fwDOM);
  100.  
  101.     // Now restore the selection to the body before doing the insert.
  102.     restoreBodyRelativeSelection();
  103.     docDOM.insertHTML(theHTML, true);
  104.   }
  105.  
  106.   return validFWFile;
  107. }
  108.  
  109. // These functions were copied from Objects/Server/serverObjectsCommon.js
  110. // because it didn't make sense to include that file here.
  111. //
  112. //--------------------------------------------------------------------
  113. // FUNCTION:
  114. //   saveBodyRelativeSelection
  115. //
  116. // DESCRIPTION:
  117. //   Stores the body tag relative location of the current selection
  118. //   in the global variable CURRENT_SEL.
  119. //
  120. // ARGUMENTS:
  121. //   none
  122. //
  123. // RETURNS:
  124. //   nothing
  125. //--------------------------------------------------------------------
  126.  
  127. var CURRENT_SEL = null;
  128.  
  129. function saveBodyRelativeSelection()
  130. {
  131.   var dom = dw.getDocumentDOM();
  132.   
  133.   var sel = dom.getSelection();
  134.   
  135.   if (sel && sel.length > 1)
  136.   {
  137.     var bodyOffset = dom.nodeToOffsets(dom.body);
  138.  
  139.     sel[0] = sel[0] - bodyOffset[0];
  140.     sel[1] = sel[1] - bodyOffset[0];
  141.     
  142.     CURRENT_SEL = sel;
  143.   }  
  144. }
  145.  
  146.  
  147. //--------------------------------------------------------------------
  148. // FUNCTION:
  149. //   restoreBodyRelativeSelection
  150. //
  151. // DESCRIPTION:
  152. //   Sets the selection back to its original location, before any
  153. //   head edits were made.
  154. //
  155. // ARGUMENTS:
  156. //   none
  157. //
  158. // RETURNS:
  159. //   nothing
  160. //--------------------------------------------------------------------
  161.  
  162. function restoreBodyRelativeSelection()
  163. {
  164.   var sel = CURRENT_SEL;
  165.   CURRENT_SEL = null;
  166.   
  167.   if (sel)
  168.   {
  169.     var dom = dw.getDocumentDOM();
  170.     
  171.     var bodyOffset = dom.nodeToOffsets(dom.body);
  172.     
  173.     sel[0] = sel[0] + bodyOffset[0];
  174.     sel[1] = sel[1] + bodyOffset[0];
  175.     
  176.     dom.setSelection(sel[0], sel[1]);
  177.   }
  178. }
  179.  
  180.